import { _decorator, Component, Node, Label, Sprite, clamp01 } from 'cc';
import { BasicUI } from '../../Init/Basic/BasicUI';
import { EventTypes } from '../../Init/Managers/EventTypes';
import { AudioEnum } from '../../Init/SystemAudio/AudioEnum';
import { AudioSystem } from '../../Init/SystemAudio/AudioSystem';
import { PlatformType, SDKSystem } from '../../Init/SystemSDK/SDKSystem';
import { StorageSystem } from '../../Init/SystemStorage/StorageSystem';
import { UIEnum } from '../../Init/SystemUI/UIEnum';
import { UISystem } from '../../Init/SystemUI/UISystem';
import { Adsdk } from '../../myScript/Adsdk';
const { ccclass, property } = _decorator;

@ccclass('ResurgenceUI')
export class ResurgenceUI extends BasicUI {

    _isGuideLv = false;

    public show(d?) {
        super.show(d);
        //是否新手关
        this._isGuideLv = StorageSystem.getData().levelAssets.curLv > 2;
        this.resetTime();
        this.resetWxAd();

        Adsdk.instance.hideAllAd();
        Adsdk.instance.showInterstitialAd();
        Adsdk.instance.showGridAdTop();
        Adsdk.instance.showBanner();
        Adsdk.instance.showGridAdLeftOne();
        Adsdk.instance.showGridAdRightOne();
    }

    public hide(d?) {
        super.hide(d);
    };

    public update(dt) {
        this.updateTime(dt);
    }

    // #region ----------------按钮---------
    // 关闭
    public onCloseClick() {
        AudioSystem.playEffect(AudioEnum.BtnClick);
        this._isFinish = true;
        this.emit(EventTypes.RoleEvents.CanceResurgence);
        UISystem.hideUI(UIEnum.ResurgenceUI);

        Adsdk.instance.hideAllAd();
    }
    //视频
    public onResurgenceClick() {
        //倒计时暂停
        this._isFinish = true;
        AudioSystem.playEffect(AudioEnum.BtnClick);

        this.emit(EventTypes.SDKEvents.ShowVideo, {
            success: () => {
                //观看视频成功
                this.emit(EventTypes.RoleEvents.Resurgence);
                UISystem.hideUI(UIEnum.ResurgenceUI);
            },
            fail: () => {
                this._isFinish = false;
            },
            cancel: () => {
                this._isFinish = false;
            },
        })
    }

    // #endregion


    // #endregion -------------倒计时-------------
    @property(Sprite)
    protected progressSprite: Sprite = null;

    @property(Label)
    protected timeLabel: Label = null;
    private _curt = 0;
    private _isFinish = false;
    private _secend = 0;
    public resetTime() {
        this._curt = 5.99;
        this._secend = 5;
        this._isFinish = false;
        this.timeLabel.string = '5';
        this.progressSprite.fillRange = 1;
    }

    public updateTime(dt) {
        if (!this._isFinish) {
            this._curt -= dt;
            let _secend = Math.floor(this._curt);
            if (_secend <= 0) {
                this._isFinish = true;
                this.onFinish();
            } else {
                if (this._secend != _secend) {
                    this._secend = _secend;
                    //激活动画
                    this.timeLabel.node.active = false;
                    this.timeLabel.node.active = true;
                    //
                    this.checkWXAd_insert();
                    console.log('_secend: ', _secend)
                }
                this.timeLabel.string = this._secend.toFixed(0);
            }
            this.progressSprite.fillRange = clamp01(this._secend / 5);
        }
    }

    private onFinish() {
        UISystem.hideUI(UIEnum.ResurgenceUI);
        //进入取消复活
        this.emit(EventTypes.RoleEvents.CanceResurgence);
    }

    // #region


    // #region ------------WX 广告逻辑-------------
    private _wxInsertShowed = false;

    private resetWxAd() {
        this._wxInsertShowed = false;
    }

    //插屏广告
    private checkWXAd_insert() {
        //判断平台
        if (this._wxInsertShowed ||
            [PlatformType.WXMiniGame, PlatformType.TTMiniGame].indexOf(SDKSystem._curPlatform) < 0) return;

        //显示插屏
        if (Math.random() < 0.4) {
            console.log('复活-显示插屏:');
            this._wxInsertShowed = true;
            this.emit(EventTypes.SDKEvents.ShowInsertAd, {
                success: () => {
                    //暂停倒计时
                    this._isFinish = true;
                },
                fail: () => {
                    this._isFinish = false;
                    this.onWxInsertClosed();
                },
                close: () => {
                    this._isFinish = false;
                    this.onWxInsertClosed();
                }
            })
        }
    }
    //插屏关闭/或者加载失败之后
    private onWxInsertClosed() {

    }

    // #endregion

}

